<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class CateService extends BaseService
{
    //分类

    protected $typeCase = 2; //原创案例分类

    //案例选项
    public function caseOption()
    {
        $arys = ['cate' => []];
        $res = $this->dbConn($this->dbCate)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', $this->typeCase)
            ->where('pid', 0)
            ->get();
        $arys['cate'] = $res;
        return $arys;
    }

    /**
     * 案例列表
     * @param int $pid 父级ID
     */
    public function caseIndex($pid = 0)
    {
        $db = $this->dbConn($this->dbCate)
            ->where('type', $this->typeCase)
            ->where('del', 0);
        if ($pid) {
            $db = $db->where('pid', $pid);
        }
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            //分类信息
            $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $v['pid'])->first();
            $pname = empty($cateOne) ? '-' : $cateOne['title'];
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'pid' => $v['pid'],
                'pname' => $pname,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 案例添加
     * @param array $pas
     */
    public function caseAdd($pas)
    {
        $res = (array) $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('type', $this->typeCase)
            ->where('pid', $pas['pid'])
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '该分类名称已存在');
        }
        $pas['type'] = $this->typeCase;
        $pas['addtime'] = $pas['uptime'] = toDate();
        $lastid = $this->dbConn($this->dbCate)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    /**
     * 案例修改
     * @param int $id
     * @param array $pas
     */
    public function caseModify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('type', $this->typeCase)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '该分类不存在');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbCate)->where('id', $id)
            ->update($pas);
        if (!$up) {
            return $this->toback(-1, '修改失败');
        }
        return $this->toback();
    }

    /**
     * 案例删除
     * @param int $id
     */
    public function caseDel($id)
    {
        $res = (array) $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('type', $this->typeCase)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '该分类不存在');
        }
        $up = $this->dbConn($this->dbCate)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}